package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.query.BusCustomerQuery;
import cn.wolfcode.car.business.domain.BusCustomer;
import cn.wolfcode.car.business.mapper.BusCustomerMapper;

import cn.wolfcode.car.business.service.IBusCustomerService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.List;

@Service()
@Transactional
public class BusCustomerServiceImpl implements IBusCustomerService {

    @Autowired
    private BusCustomerMapper busCustomerMapper;
    @Override
    public void save(BusCustomer busCustomer) {
        List<BusCustomer> list = this.list();
        for (BusCustomer busCustomer2 : list) {
            if (busCustomer2.getPhone().equals(busCustomer.getPhone())){
                throw new BusinessException("该电话以存在");
            }
        }
        busCustomerMapper.insert(busCustomer);
    }

    @Override
    public void update(BusCustomer BusCustomer) {
        busCustomerMapper.updateByPrimaryKey(BusCustomer);
    }

    @Override
    public BusCustomer get(Long id) {
        return busCustomerMapper.selectByPrimaryKey(id);
    }




    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            busCustomerMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<BusCustomer> list() {
        return busCustomerMapper.selectAll();
    }

    @Override
    public TablePageInfo<BusCustomer> query(BusCustomerQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<BusCustomer> busCustomers = busCustomerMapper.selectForList(qo);
        return new TablePageInfo<BusCustomer>(busCustomers);
    }

    @Override
    public Workbook export(HttpServletResponse response) {
        Workbook wk= new XSSFWorkbook();
        List<BusCustomer> list = this.list();
        Sheet sheet = wk.createSheet("人员");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("联系方式");
        row.createCell(3).setCellValue("生日");
        row.createCell(4).setCellValue("是否vip");
        row.createCell(5).setCellValue("备注信息");
        for (int i=0;i<list.size();i++){
            Row row1 = sheet.createRow(i + 1);
            row1.createCell(0).setCellValue(list.get(i).getId());
            row1.createCell(1).setCellValue(list.get(i).getName());
            row1.createCell(2).setCellValue(list.get(i).getPhone());
            SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
            String format = sm.format(list.get(i).getBirthday());
            row1.createCell(3).setCellValue(format);

            BusCustomer busCustomer = list.get(i);
            boolean vip = busCustomer.isVip();
            String s = vip ? "是" : "否";
            row1.createCell(4).setCellValue(s);
            row1.createCell(5).setCellValue(list.get(i).getRemark());
        }
        return wk;
    }


}
